// JavaScript Document

var ShareTitle	=	'新春报道 祝福豹到',
	ShareDesc 	=	'2015新春豹到！文字祝福不过瘾，玩转语音才够COOL！', 
	ShareImg = 'http://wechat-dev.jaguar.inzen.com.cn/ts/s/img/ico.jpg',
	ShareUrl  = 'http://wechat-dev.jaguar.inzen.com.cn/ts/s/';

// Created By Nicky
var SH,SW,oldX=0,oldY=0,frameTimer=null;
var tcount=0,ccount=0 , loadtimer=null , loadcoy=0;

for(i=0;i<6;i++){
	$('#load_pic').append('<img src="img/loadimg/loadbg'+i+'.png" class="ab w h ld_img'+i+'" />');
}

loadtimer = setInterval(function(){ 
	if(loadcoy<6){
		$('#load_pic img').hide(); 
		$('#load_pic img.ld_img'+loadcoy).show(); 
	}
	loadcoy = loadcoy+1;
	if(loadcoy>20){
		loadcoy =0;
	}
},100);

var FRAME_IMG_DATA = [
	{src:"img/card/b (#).png",count:148} ,
	{src:"img/snow/snow#.png",count:4} 
];

var IMG_ARRAY = [];

/* 动画场景设置 */
var FrameConfig = {
		timer:null,
		pid:'framebox',
		src:"img/card/b (#).png",
		timespan:120,
		frameCount:148,
		className:'ab w hd',
		step:1,
 
		pause:false,
		stop:false,
		
		curFrame:1,
		step_conf:{}
};
//敲门动画
FrameConfig.step_conf[1] = {start:1,end:44,loop:true, Event:{ Loop:function(){ }} }; 
//进场动画
FrameConfig.step_conf[2] = {start:45,end:64,loop:false,Event:{ End:function(){ playFrame(3); }}};
//提示点击
FrameConfig.step_conf[3] = {start:65,end:69,loop:true,Event:{ FirstLoop:function(){ 
	$('#vplaybtn,#vplaybtnsc,#vplaybtnbg').width(SW*0.26).height(SW*0.26).css({'left':SW*0.37,'top':SH*0.47}).fadeIn(); 
	$('#vplaybtn,#vplaybtnsc,#vplaybtnbg').bind('touchstart',function(ev){ 
	 	ev.preventDefault(); 
		$('#vplaybtn,#vplaybtnsc,#vplaybtnbg').fadeOut();
		playFrame(4);
	});
} }};
//鞠躬动画
FrameConfig.step_conf[4] = {start:70,end:82,loop:false, Event:{ End:function(){ playFrame(5); }}};
//说话动画
FrameConfig.step_conf[5] = {start:83,end:132,loop:true, Event:{ FirstLoop:function(){ }}}; 
//说话动画

FrameConfig.step_conf[6] = {start:133,end:148,loop:false, Event:{ End:function(){ $('#torecord').fadeIn();  } }}; 

function InitFrame(){
	$('#'+FrameConfig.pid).empty();
	for(i=1;i<=FrameConfig.frameCount;i++){ 
		html = '<img class="'+FrameConfig.pid+'_'+i +' '+ FrameConfig.className +'" src="'+FrameConfig.src.replace(/#/,i)+'">';
		$('#'+FrameConfig.pid).append(html);
	}
}

function playFrame(step){
	//场景信息
	FrameConfig.step = step;
	FrameConfig.pause=false;
	FrameConfig.stop=false;
	
	//场景参数信息
	var Step_Info = FrameConfig.step_conf[FrameConfig.step];
	var START_FRAME = Step_Info['start'];
	var END_FRAME = Step_Info['end'];
	var LOOP = Step_Info['loop'];
	
	var loopCount = 0;
	//播放场景动画
	FrameConfig.curFrame = START_FRAME;
	
	clearInterval(FrameConfig.timer);
	try{
		Step_Info.Event.Start(); 
	}catch(e){}
	FrameConfig.timer = setInterval(function(){
		if(!FrameConfig.pause&&!FrameConfig.stop){
			$('#'+FrameConfig.pid+" img").hide();
			$('#'+FrameConfig.pid+" ."+FrameConfig.pid+"_"+FrameConfig.curFrame).show();
			FrameConfig.curFrame++; 
			if(FrameConfig.curFrame>=END_FRAME){
				if(LOOP){
					FrameConfig.curFrame = START_FRAME;
					try{
						Step_Info.Event.Loop(); 
					}catch(e){}
					if(loopCount<1){
						try{
							Step_Info.Event.FirstLoop(); 
						}catch(e){}
					}
					loopCount++;
					if(Step_Info.loopCount==loopCount){
						FrameConfig.pause = true;
						try{
							Step_Info.Event.End(); 
						}catch(e){}
					}
				}else{
					FrameConfig.pause = true;
					try{
						Step_Info.Event.End(); 
					}catch(e){}
				}
			}
		}
	},FrameConfig.timespan);
}

$(function(){
	InitPage();
	$('body').unbind('touchmove').bind('touchmove',function(ev){ ev.preventDefault(); });
	//预加载页面图片 
});

wx.ready(function () {
	$.each($('img'),function(i,d){
		IMG_ARRAY[tcount++] = $(d).attr('src');
	});
	document.getElementById('clickdoor').play();
	$('.music_btn').addClass('music_btn_on');
	//预加载Frame图片
	$.each(FRAME_IMG_DATA,function(k,d){
		for(i=1;i<d.count+1;i++){ IMG_ARRAY[tcount++] = d.src.replace(/#/,i); }
	});
	var images = [];
	$.each(IMG_ARRAY,function(i,d){
		 images[i] = new Image();
         images[i].onload = function(){
            ccount++;
			$('#load_txt').text(parseInt(100*ccount/tcount)+'%');  
			if(ccount==tcount) setTimeout(function(){ $('#load').fadeOut();  LoadCallBack(); clearInterval(loadtimer); },1000); 
       }
       images[i].src = d; 
	});
	 
	resetShare();
	//页面载入完成后 下载语音
	wx.downloadVoice({
		serverId: voice.serverId,
			//语音下载完成
			success: function (res) {
				voice.localId =  res.localId; 
			},
			//语音已过期
			fail: function (res) { 
				$('#door').hide();
				$('#outtime , #logo').fadeIn();
				$('#overimg').css('right',SW+10).animate({ right: 0 }, 500 );
				setTimeout(function(){ $('#outtimediv').fadeIn(); },600);
			}
	}); 
     $('#vplaybtn').bind('touchstart',function(){
		$('#torecord').fadeOut();
		//document.getElementById('clickdoor').pause();
		setTimeout(function(){
			wx.playVoice({
				localId: voice.localId
			});
		},1000);
		_smq.push(['custom','2015CNY','Start2']);
     }); 
	 wx.onVoicePlayEnd({
		complete: function (res) {
			//$('#vplaybtn,#vplaybtnsc,#vplaybtnbg,#torecord').fadeIn(); 
			//playFrame(3);
			playFrame(6);
		}
  	 });
  });
  
  
function resetShare(){
	wx.onMenuShareAppMessage({
	      title: ShareTitle,
	      desc: ShareDesc,
	      link: ShareUrl,
	      imgUrl: ShareImg,
	      trigger: function (res) { /* alert('用户点击发送给朋友'); */ },
	      success: function (res) {  /* alert('已分享'); */ },
	      cancel: function (res) { /* alert('已取消'); */ },
	      fail: function (res) { /* alert(JSON.stringify(res)); */ }
	 }); 
	 
	 wx.onMenuShareTimeline({ // 2.2 监听"分享到朋友圈"按钮点击、自定义分享内容及分享结果接口
	      title: ShareDesc, 
	      link: ShareUrl, 
	      imgUrl: ShareImg,
	      trigger: function (res) { /* alert('用户点击分享到朋友圈'); */ },
	      success: function (res) {  /* alert('已分享'); */ },
	      cancel: function (res) { /* alert('已取消'); */ },
	      fail: function (res) { /* alert(JSON.stringify(res)); */ }
	 }); 
}

function LoadCallBack(){
	$('#music_btn').fadeIn(); 
	window.onorientationchange = orientationChange; 
	$('#sc').html('<table><tr><td><img id="sc_img" src="img/sc.png" /></td></tr></table>');
	orientationChange(); 
	//playFrame({pid:'framebox',src:'img/click/c (#).png',loop:true,timespan:80,framecount:89,className:'ab w h hd'});
	$('.click_btn').bind('touchend',function(){
		//$('#framebox').html('');
		playFrame(2);
		//document.getElementById('clickdoor').pause();
		setTimeout(function(){ $('#logo').fadeIn(); },2000);
		$('#door').animate({ left:SW+3 }, 1500); 
		_smq.push(['custom','2015CNY','Open']);
	});  
	//$('#door').unbind('touchmove').bind('touchmove',function(ev){ ev.preventDefault(); document.getElementById('clickdoor').play(); $('#logo').fadeIn();  });
	$('#torecordBtn , .torecordBtn1').bind('touchstart',function(){ location.href="index.php"; });
	
	$('.music_btn').bind('dblclick',function(ev){ ev.preventDeafult(); }).bind('touchstart',function(){ 
		if($(this).hasClass('music_btn_on')){
			$(this).removeClass('music_btn_on');
			document.getElementById('clickdoor').pause();
		}else{
			$(this).addClass('music_btn_on');
			document.getElementById('clickdoor').play();
		}
	});

	$('#framebox , #opendoor').fadeIn(); 
	InitFrame();
	playFrame(1);

}

//初始化界面 获取宽和高
function InitPage(){
	SW = $(window).width();
	SH = $(window).height();
}

//手机屏幕旋转
function orientationChange(){ 
	InitPage();	 
	if(window.orientation==180||window.orientation==0){  
		$('#sc').fadeOut();
		$('#vplaybtn,#vplaybtnsc,#vplaybtnbg').width(SW*0.26).height(SW*0.26).css({'left':SW*0.37,'top':SH*0.47});
	}
	if(window.orientation==90||window.orientation==-90){ 
		$('#sc').fadeIn();
	}
}